package RJZ.Rainbow.Service;

import RJZ.Rainbow.Config.Information;
import RJZ.Rainbow.Controller.UIConruoller;
import RJZ.Rainbow.Dao.ChequerDao;
import RJZ.Rainbow.Dao.OrderDao;
import RJZ.Rainbow.entity.Chequer;
import RJZ.Rainbow.entity.Order;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
@Service
public class OrderService {
    @Autowired
    Information imfor;
    @Autowired
    OrderDao orderDao;
    PrintWriter out;
    @Autowired
    ChequerDao chequerDao;
    @Autowired
    UIConruoller uiConruoller;

    public void init(PrintWriter out) {
        this.out = out;
    }
    public void OrderOutput(String orderid,String who){
        Order order=orderDao.GetOrderByID(orderid);
        if(order != null){
           if(who.equals("0")){
               Print("121",orderid+"取件人开箱");
           } else if (who.equals("1")) {
               Print("121",orderid+"投件人开箱");
           }
           chequerDao.UpdateChequerStatus(3,order.getChequer_id());
            String command = "{" +
                    "\"Cop\":" + "\"150\"," +
                    "\"door\":" + "\"" + order.getChequer_id() + "\"" +
                    "}" + '\n';
            out.println(command);
            //发送开箱命令
            out.flush();//

        }
        else {
            uiConruoller.getTips().tip("没有该订单信息");
        }
    }
    public void OrderInput(String orderid){
        Print("111",orderid);
        //System.out.println("订单"+orderid+"验证成功");
        Chequer chequer=chequerDao.GetEmptyChequer();
        if(chequer != null){
            chequerDao.UpdateChequerStatus(1,chequer.getChequer_id());
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
            String time=sDateFormat.format(new java.util.Date()).replace("-", "");
            if(orderDao.GetOrderByID(orderid)==null){
                orderDao.OrderInput(orderid,time,chequer.getChequer_id());
                String command = "{" +
                        "\"Cop\":" + "\"140\"," +
                        "\"door\":" + "\"" + chequer.getChequer_id() + "\"" +
                        "}" + '\n';
                out.println(command);
                out.flush();//
            }else {
                //System.out.println(orderDao.GetOrderByID(orderid));
                uiConruoller.getTips().tip("订单"+orderid+"已入库，请勿重复扫描");}
        }else {
            uiConruoller.getTips().tip("当前无空格子存储");
        }
    }
    public void SubCloseTheDoor(int chequerid, Channel ch){
        Chequer chequer=chequerDao.GetChequerByID(chequerid);
        if(chequer != null){
            if (chequer.getUsing_or_not()==1) {
                chequerDao.UpdateChequerStatus(2,chequerid);
                uiConruoller.getTips().tip(chequerid+"投件关门成功,正在回传服务器");
                Order order = orderDao.GetOrderByDoor(chequerid);
                if(order != null){
                    uiConruoller.getTips().tip("入库成功");
                    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
                    String time=sDateFormat.format(new java.util.Date()).replace("-", "");
                    String orderid=order.getTable_id();
                    orderDao.OrderMailIn(time,orderid);
                    String msg = "{" +
                            "\"Cop\":" + "\"170\","
                            + "\"box_id\":" + "\"" + imfor.getId() + "\","
                            + "\"order_id\":" + "\"" + orderid + "\","
                            + "\"door\":" + "\"" + chequerid + "\""
                            + "}" + '\n';
                    ch.writeAndFlush(msg);}
                else {
                    uiConruoller.getTips().tip("订单不存在，无法执行投件关门操作");
                    chequerDao.UpdateChequerStatus(chequer.getUsing_or_not(),chequerid);
                }
            }
            else {
                uiConruoller.getTips().tip("发生门序错误:当前为"+chequer.getUsing_or_not()+"\n调整类型为：投件关闭门");
            }

        }
    }
    public void TakeCloseTheDoor(int chequerid,Channel ch){
        Chequer chequer=chequerDao.GetChequerByID(chequerid);
        System.out.println("执行取件关门操作"+ chequer.toString());
        if(chequer != null){
            if (chequer.getUsing_or_not()==3) {
                uiConruoller.getTips().tip(chequerid+"取件关门成功,正在回传服务器");
                chequerDao.UpdateChequerStatus(0,chequerid);
                Order order = orderDao.GetOrderByDoor(chequerid);
                System.out.println(order);
                if(order != null){
                    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
                    String time=sDateFormat.format(new java.util.Date()).replace("-", "");
                    orderDao.OrderOutput(time,order.getTable_id());
                    String msg = "{" +
                            "\"Cop\":" + "\"175\","
                            + "\"box_id\":" + "\"" + imfor.getId() + "\","
                            + "\"order_id\":" + "\"" + order.getTable_id() + "\""
                            + "}" + '\n';
                    ch.writeAndFlush(msg);
                }else {
                    uiConruoller.getTips().tip("订单不存在，无法执行投件关门操作");
                    chequerDao.UpdateChequerStatus(chequer.getUsing_or_not(),chequerid);
                }
            }
        }
    }
    public void UpdateLocalTemperature(int chequerid, int temp, Channel ch){
        Chequer chequer=chequerDao.GetChequerByID(chequerid);
        if(chequer != null){
            chequerDao.UpdateChequerTemperature(temp,chequerid);
            String msg = "{" +
                    "\"Cop\":" + "\"160\","+
                    "\"order_id\":\"" +orderDao.GetOrderByDoor(chequerid).getTable_id() +"\","+
                    "\"box_id\":\"" + imfor.getId()+"\","+
                    "\"temp\":\"" + temp+"\","+
                    "}" + '\n';
            ch.writeAndFlush(msg);

        }else {
            uiConruoller.getTips().tip("格子不存在");
        }
    }
    public void Print(String cop,String msg){
        uiConruoller.getTips().match(cop, msg);
    };
}
